<?php
namespace app\index\controller;

use app\index\controller\Base;
use think\Db;
use think\Session;
use think\Request;

class Index extends Base
{
    /**
     * 会员首页
     * @return mixed
     */
    public function Index()
    {
//        echo '<h1 style="text-align:center">系统正在维护!</h1>';die;
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        
        $ip = Request::instance()->ip();
        $logininfo['ip'] = $ip;
        $logininfo['user_id'] = $UserInfo['id'];
        $logininfo['time'] = date('Y-m-d H:i:s');
        $logininfo['type'] = 2;//前台访问
        Db::name('user_login')->insert($logininfo);
//        //轮播图
//        $BannerList = Db::name('banner')->where(['place'=>1])->select();
//        $this->assign('BannerList',$BannerList);
        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        //推荐的父级分类
        $ClassList = Db::name('goods_class')->where(['son'=>0,'is_recommend'=>1])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['Goods'] = Db::name('goods')->where(['parent_id'=>$ClassList[$k]['id'],'is_shelf'=>2,'goods_type'=>1])->select();
        }
        $this->assign('ClassList',$ClassList);

        //升级产品
        $InvestGoods = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>2])->order('xu asc')->select();
        $this->assign('InvestGoods',$InvestGoods);

        //推荐的产品
        $GoodsList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>1])->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
        
        //复购产品
        //推荐的产品
        $jinList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>3])->order('xu asc')->select();
        $this->assign('jinList',$jinList);
        
        //秒杀产品
        //推荐的产品
        $miaoList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>4])->order('xu asc')->select();
        
        // var_dump(time());die;
        
        
        //倒计时
        $aa = [];
        foreach ($miaoList as $v){
            if($v['etime'] == ''){
                $v['time'] = 0;
                $aa[] = $v;
            }else{
                // $v['time'] = date('Y-m-d',$v['etime']);
                $v['time'] = $v['etime'];
                $aa[] = $v;
            }
        }
        
        // echo '<pre>';
        // var_dump($aa);die;
        $this->assign('miaoList',$aa);
        $nowtime = time();
        $this->assign('nowtime',$nowtime);

        return $this->fetch('Index');
    }

    /**
     * 应用列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function App(){
        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        //应用列表
        $AppList = Db::name('app')->where(['is_show'=>1])->select();
        $this->assign('AppList',$AppList);
        
        //推荐的产品
        $GoodsList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2])->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
        
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);

        return $this->fetch('App');
    }

    /**
     * 应用详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function AppDetail(){
        //应用信息
        $AppInfo = AppInfo(['id'=>Param('id')]);
        $this->assign('AppInfo',$AppInfo);

        return $this->fetch('AppDetail');
    }
    
    public function pin(){
        //        //轮播图
//        $BannerList = Db::name('banner')->where(['place'=>1])->select();
//        $this->assign('BannerList',$BannerList);
        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        //推荐的父级分类
        $ClassList = Db::name('goods_class')->where(['son'=>0,'is_recommend'=>1])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['Goods'] = Db::name('goods')->where(['parent_id'=>$ClassList[$k]['id'],'is_shelf'=>2,'goods_type'=>1])->select();
        }
        $this->assign('ClassList',$ClassList);

        //升级产品
        $InvestGoods = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>2])->order('xu asc')->select();
        $this->assign('InvestGoods',$InvestGoods);

        //推荐的产品
        $GoodsList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>1])->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
        
        //复购产品
        //推荐的产品
        $jinList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>3])->order('xu asc')->select();
        $this->assign('jinList',$jinList);
        
        //秒杀产品
        //推荐的产品
        $pinList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>5])->order('xu asc')->select();
        $this->assign('pinList',$pinList);
        // var_dump($pinList);die;
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);

        return $this->fetch('pin');
    }
    
    public function pt(){

        //推荐的父级分类
        $ClassList = Db::name('goods_class')->where(['son'=>0,'is_recommend'=>1])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['Goods'] = Db::name('goods')->where(['parent_id'=>$ClassList[$k]['id'],'is_shelf'=>2,'goods_type'=>1])->select();
        }
        $this->assign('ClassList',$ClassList);

        //升级产品
        $InvestGoods = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>2])->order('xu asc')->select();
        $this->assign('InvestGoods',$InvestGoods);

        //推荐的产品
        $GoodsList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>1])->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
        
        //复购产品
        //推荐的产品
        $jinList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>3])->order('xu asc')->select();
        $this->assign('jinList',$jinList);
        
        //秒杀产品
        //推荐的产品
        $pinList = Db::name('goods')->where(['is_recommend'=>2,'is_shelf'=>2,'goods_type'=>5])->order('xu asc')->select();
        $this->assign('pinList',$pinList);
        // var_dump($pinList);die;
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        
        //拼团列表
        $list = Db::name('user_pt')->where(['status'=>1])->select();
        $list = db('user_pt')
    	->alias('m')
    	->join('goods a','m.goods_id = a.id')
        ->where(['m.status'=>1])//条件:状态为1
        ->order('m.id desc')//根据id降序排列
    	->select();
    // 	echo '<pre>';
    //     var_dump($list);die;
        $this->assign('list',$list);
        return $this->fetch('pt');
    }
    
    /**
     * 商品详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsDetail(){
        //商品ID
        $GoodsID = Param('goods_id');
        $pt_id = Param('pt_id');//拼团编号
        $this->assign('pt_id',$pt_id);
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        //商品信息
        $GoodsInfo = GoodsInfo(['id'=>$GoodsID]);
        // var_dump($GoodsInfo);die;
        // var_dump($GoodsInfo);die;
        $GoodsInfo['album'] = explode(",",$GoodsInfo['album']);
        $this->assign('GoodsInfo',$GoodsInfo);
        //同大类产品
        $GoodsList = Db::name('goods')->where(['parent_id'=>$GoodsInfo['parent_id'],'is_shelf'=>2])->select();
        $this->assign('GoodsList',$GoodsList);
        //倒计时
        if($GoodsInfo['etime'] == ''){
            $time = 0;
        }else{
            $time = date('Y-m-d H:i:s',$GoodsInfo['etime']);
        }
        $this->assign('time',$time);
        $nowtime = time();
        $this->assign('nowtime',$nowtime);
        
        $ptinfo = Db::name('user_pt')->where(['pt_id'=>$pt_id])->find();
        $this->assign('ptinfo',$ptinfo);
        return $this->fetch('GoodsDetail');
    }
}
